package controllers;

import java.util.List;

import play.mvc.Controller;
import models.Customer;

public class Customers extends Controller {
	/**
	 * 添加客户
	 * 
	 * @param customer
	 */
	public static void addCustomer() {
		render();	
	}
	public static void saveCustomer(Customer customer) {
		customer.save();
		listCustomer();		
	}

	/**
	 * 修改客户
	 * 
	 * @param customerID
	 */
	public static void editCustomer(long customerID) {
		Customer customer = Customer.find("id=?", customerID).first();
		render(customer);
	}

	/**
	 * 删除客户
	 * 
	 * @param customerID
	 */
	public static void deleteCustomer(long customerID) {
		Customer customer = Customer.find("id=?", customerID).first();
		customer.delete();
		listCustomer();	
	}
	
	/**
	 * 客户列表
	 */
	public static void listCustomer(){
		List<Customer> customerList = Customer.findAll();
        render(customerList);   
	}
	
	/**
	 * 客户
	 */
	public static void search(String searchText){
		List<Customer> customerList = Customer.find("customername = ?",searchText).fetch();
		render(customerList);
	}
	
	public static void searchBy(String searchText, String by){
		Customer customer = Customer.find(by+" = ?",searchText).first();
		renderJSON(customer);
	}

}
